package com.hh.admin.controller;

import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hutool.core.util.ObjectUtil;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hh.admin.entity.SysUser;
import com.hh.admin.exception.ValidEntityException;
import com.hh.admin.service.ISysRoleService;
import com.hh.admin.service.ISysUserService;
import com.hh.admin.view.SysUserView;
import com.hh.core.annotion.OperationLog;
import com.hh.core.common.BaseController;
import com.hh.core.entity.ResultEntity;
import com.hh.core.enums.OperationLogEnum;
import com.hh.core.log.LogTempObject;
import com.hh.core.shiro.util.ShiroUtil;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hxn
 * @since 2018-01-16
 */
@Controller
@RequestMapping("/admin/sysUser")
public class SysUserController extends BaseController {

	@Autowired
	ISysUserService sysUserService;

	@Autowired
	ISysRoleService sysRoleService;

	/**
	 * 
	 * @Title: user
	 * @Description: 跳转到用户页面
	 * @param @param model
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @author hxn
	 * @date 2018年2月5日 下午2:19:45
	 * @throws
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	public String user(Model model) {
		return SysUserView.USER;
	}

	/**
	 * 
	 * @Title: userAdd
	 * @Description: 用户添加页
	 * @param @param model
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @author hxn
	 * @date 2018年2月5日 下午2:19:56
	 * @throws
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String userAdd(Model model) {
		model.addAttribute("action", "add");
		Map<String, Object> columnMap = new HashMap<String, Object>();
		columnMap.put("is_enable", 1);
		model.addAttribute("roleList", sysRoleService.selectByMap(columnMap));
		return SysUserView.USER_DETAIL;
	}

	/**
	 * 
	 * @Title: addUser
	 * @Description: 用户信息添加
	 * @param @param user
	 * @param @param result
	 * @param @return 设定文件
	 * @return ResultEntity 返回类型
	 * @author hxn
	 * @date 2018年2月5日 下午2:20:06
	 * @throws
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	@OperationLog(value="添加用户",type=OperationLogEnum.ADD)
	public ResultEntity addUser(@Valid SysUser user, BindingResult result,
			Long groupId, @RequestParam(value = "roleId[]")Long[] roleId) {
		if (result.hasErrors() || ObjectUtil.isNull(groupId)
				|| ObjectUtil.isNull(roleId)) {
			throw new ValidEntityException();
		}
		user.setSalt(ShiroUtil.getRandomSalt(5)); // 设置密码盐
		user.setPassword(ShiroUtil.md5(user.getPassword(), user.getSalt()));
		sysUserService.addUser(user, groupId, roleId);
		return successResult("添加成功!");
	}

	/**
	 * 
	 * @Title: userEdit
	 * @Description: 用户修改页
	 * @param @param id
	 * @param @param model
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @author hxn
	 * @date 2018年2月5日 下午2:20:16
	 * @throws
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String userEdit(@PathVariable Long id, Model model) {
		SysUser user = sysUserService.getById(id);
		model.addAttribute("user", user);
		Map<String, Object> columnMap = new HashMap<String, Object>();
		columnMap.put("is_enable", 1);
		model.addAttribute("roleList", sysRoleService.selectByMap(columnMap));
		model.addAttribute("action", "edit");
		LogTempObject.me().setObject(user);
		return SysUserView.USER_DETAIL;
	}

	/**
	 * 
	 * @Title: editUser
	 * @Description: 用户信息修改
	 * @param @param user
	 * @param @param result
	 * @param @return 设定文件
	 * @return ResultEntity 返回类型
	 * @author hxn
	 * @date 2018年2月5日 下午2:20:25
	 * @throws
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	@OperationLog(value="修改用户",type=OperationLogEnum.EDIT,paramIndex={0,2,3})
	public ResultEntity editUser(@Valid SysUser user, BindingResult result,
			Long groupId, @RequestParam(value = "roleId[]")Long[] roleId) {
		if (result.hasErrors()) {
			throw new ValidEntityException();
		}
		sysUserService.updateById(user,groupId,roleId);
		return successResult("修改成功!");
	}

	/**
	 * 
	 * @Title: deleteUser
	 * @Description: 用户信息删除
	 * @param @param id
	 * @param @return 设定文件
	 * @return ResultEntity 返回类型
	 * @author hxn
	 * @date 2018年2月5日 下午2:20:34
	 * @throws
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	@OperationLog(value="删除用户",type=OperationLogEnum.DELETE)
	public ResultEntity deleteUser(@NotBlank(message = "ID不能为空") Long id) {
		sysUserService.deleteById(id);
		return successResult("删除成功!");
	}

	/**
	 * 
	 * @Title: userList
	 * @Description: 用户分页列表
	 * @param @param pageSize
	 * @param @param pageNumber
	 * @param @return 设定文件
	 * @return JSONObject 返回类型
	 * @author hxn
	 * @date 2018年2月5日 下午3:27:35
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public JSONObject userList(int pageSize, int pageNumber,SysUser user) {
		Page<SysUser> page = new Page<SysUser>();
		page.setSize(pageSize);
		page.setCurrent(pageNumber);
		EntityWrapper<SysUser> entityWrapper = new EntityWrapper<>();
		entityWrapper.setEntity(user);
		page = sysUserService.selectPage(page,entityWrapper);
		return pageJosn(page);
	}
}
